// packageB/pages/fcyd/fcyd.js
const app = getApp();
const sendMessage = require('../../../pages/home/home')
const util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    second:180,
    times:0,
    page:0,
    timer:'',
    inter:'',
    equipmentNumber:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.startInterval();
    var time = options.time;
    this.setData({
      second:time * 60
    })
    this.startCountdown();
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"反差阅读",
      openId:openId,
      times:180
    })
    util.saveRecord(this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var second = this.data.second;
    if (second ==0){
      return;
    }
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"反差阅读",
      openId:openId
    })
    util.saveRecord(this);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  upPage(){
    var page = this.data.page;
    if(page==0){
      wx.showToast({
        title: '已经是第一页了',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    page = page-1;
    console.log(page);
    this.setData({
      page:page
    })
  },
  nextPage(){
    var page = this.data.page;
    page = page+1;
    if (page ==5){
      page =0;
    }
    console.log(page);
    this.setData({
      page:page
    })
  },
  startCountdown() {
    var that = this;
    var second = that.data.second;
    var equipmentNumber = app.nowEquipmentId;
    console.log(second);
    if (second == 0) {
      clearInterval(that.data.timer);
      clearInterval(that.data.inter);
      var score = 5;
      wx.navigateTo({
        url: '../../../pages/fraction/fraction?fraction='+score,
      })
    }
    that.data.timer = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      that.startCountdown();
      //that.startInterval();
    } , 1000)
    var times = that.data.times;
    times++;
    that.setData({
      times:times,
      equipmentNumber:equipmentNumber
    })
  },
  startInterval: function(){
    var that = this;
    that.data.inter= setInterval(function () {
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      console.log("定时任务开启");
    }, 10000)
  },
})